/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cestum.cave.presenter.wave.cursors
{
import org.goverla.collections.HashMap;
import org.goverla.collections.Stack;
import org.waveprotocol.wave.model.document.operation.AnnotationBoundaryMap;
import org.waveprotocol.wave.model.document.operation.Attributes;
import org.waveprotocol.wave.model.document.operation.BufferedDocOp;
import org.waveprotocol.wave.model.document.operation.DocInitializationCursor;

public class CaveDocumentInitCursor implements DocInitializationCursor
{
	private var elemStack:Stack = new Stack();
	private var document:BufferedDocOp;
	private var stackDepth:int;
	private var updateBlip:Function;
	private static var thisInstance:CaveDocumentInitCursor;
	private var markup:String;
	private static const STYLE:String = "style";
	
	
	public function CaveDocumentInitCursor() {
	}
	
	public static function of(manifest:BufferedDocOp, updateBlip:Function):CaveDocumentInitCursor {
		if (thisInstance == null)
			thisInstance = new CaveDocumentInitCursor();
		thisInstance.document = manifest;
		thisInstance.updateBlip = updateBlip;
		return thisInstance;
	}
	
	public function annotationBoundary(map:AnnotationBoundaryMap):void 	{
		for (var i:int=0; i < map.changeSize(); i++) {
			if (map.getChangeKey(i).indexOf(STYLE) == 0 && map.getNewValue(i) != null) {
				markup += map.getChangeKey(i).substr(6) + "='" + map.getNewValue(i) + "' "; 
			}
		}
	}
	
	public function characters(chars:String):void 	{
//		if (elemStack.  == "w:image")) {
//			currentLine.append(
//				"(image, caption=" + ConsoleUtils.renderNice(s) + ")");
//		} else {
			//currentLine.append(padding + chars);
		markup += ">" + chars + "</span>";
		updateBlip.call(this, markup );
		markup += "<span ";
//		}
	}
	
	public function elementStart(type:String, attrs:Attributes):void {
		stackDepth++;
		elemStack.enqueue(type);
		if (type == (caveRenderUtils.LINE)) {
			markup = new String("<span ");
			if (attrs.containsKey(caveRenderUtils.LINE_AUTHOR)) {
//				currentLine.remove(0, currentLine.length() - 1);
//				currentLine.append(attrs.getValue(caveRenderUtils.LINE_AUTHOR));
//				currentLine.append("::");
			}
			//lines.addItem(currentLine.toString());
			//currentLine.remove(0, currentLine.length());
		} else if (type == (caveRenderUtils.CONTRIBUTOR)) {
			if (attrs.containsKey(caveRenderUtils.CONTRIBUTOR_NAME)) {
				//displayAuthor(attrs.get(SwatWaveRenderUtils.CONTRIBUTOR_NAME));
			}
		} else if (type == (caveRenderUtils.BODY)) {
			// ignore
		} else {
			//                  throw new IllegalArgumentException(
			//                      "Unsupported element type " + type);
		}
	}
	
	public function elementEnd():void 	{
		var type:String = elemStack.dequeue() as String;
		stackDepth--;
	}
}
}